/**
 *  *2010-7-24 下午02:50:20  add 
 */
package com.base.myproject.client.ui.dialog;

import java.util.ArrayList;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;

/**
 * 多条信息在一个对话框里集中显示。
 * 	  MutiMessageItem mi1= new  MutiMessageItem("warning","aaa");
*			  
*			  MutiMessageItem mi2= new  MutiMessageItem("question","sss");
*			  MutiMessageItem mi3= new  MutiMessageItem("error","vvv");
*			  MutiMessageItem mi4= new  MutiMessageItem("info","dd");
*			  MutiMessagesDialog mmd = new MutiMessagesDialog();
*			  mmd.add(mi1);
*			  mmd.add(mi2);
*			  mmd.add(mi3);
*			  mmd.add(mi4);
*mmd.show();
 * @author Administrator
 *2010-7-24
 * javazgw@gmail.com
 */
public class MutiMessagesDialog {
	Dialog dialog = new Dialog();
	ArrayList<MutiMessageItem> al = new ArrayList<MutiMessageItem>();
	String bottomHtml;
	public MutiMessagesDialog()
	{
		init();
	}
	public MutiMessagesDialog(String bottomHtml)
	{
		this();
		this.bottomHtml = bottomHtml;
		
	}
	private void init() {
		initWidgetx();
		
	}
	private void initWidgetx() {
		dialog.setScrollMode(Scroll.AUTOY);
		dialog.setButtons(Dialog.OKCANCEL);
		//dialog.setHideOnButtonClick(true);
		dialog.setHeading("提示信息");
		dialog.getButtonById(Dialog.CANCEL).addListener(Events.Select, getListener());
		dialog.getButtonById(Dialog.OK).addListener(Events.Select, getListener());
		
	}
	
	public void add(MutiMessageItem mi)
	{
		al.add(mi);
	}
	public void show()
	{
		//LayoutContainer lc = new LayoutContainer();
		
		for(MutiMessageItem mi:al)
		{
			dialog.add(mi);
		}
		//dialog.setAutoHeight(true);
		if(bottomHtml!=null)
		{
		dialog.setBottomComponent(new Html("<hr>"+bottomHtml));
		}
		dialog.setHeight(300);
		dialog.show();
	}
	
	public void close()
	{
		dialog.hide();
	}
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;                               
                                                                     
	Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                      
				try {                                                        
					if (sync == 1) {                                           
						if (be.getType() == Events.Select) {                     
							if (be.getSource().equals(dialog.getButtonById(Dialog.CANCEL))) {                            
                                close();               
							}     
							else if(be.getSource().equals(dialog.getButtonById(Dialog.OK)))
							{
								onOk();
							}
								
						}                                                 
					}                                                          
				} finally {                                                  
					sync--;                                                    
				}                                                            
			}                                                              
                                                                     
		};                                                               
		return defaultlistener;                                          
	}
	/**
	 * 子类继承，点击确认按钮触发的事件
	 */
	public void onOk()
	{
		
	}
	public Dialog getDialog() {
		return dialog;
	}
	public void setDialog(Dialog dialog) {
		this.dialog = dialog;
	}                                                                  

}
